#include "game.h"
#include "ProcBar.h"
int x = 0;
int y = 0;

void BoardInit(int board[ROW][COL], int row, int col)
{
  memset(board, 0, sizeof(int)*row*col);
}

void BoardPrint(int board[ROW][COL], int row, int col)
{
  printf("\e[1;1H\e[2J");
  int i = 0;
  printf("  ");
  for(i = 1;i<=col;i++)
  {
    printf("%3d", i);
  }
  printf("\n");
  for(i = 0;i<row;i++)
  {
    printf("%2d ", i+1);
    int j = 0;
    for(j = 0;j<col;j++)
    {
      if(board[i][j] == 0)
      {
        printf(" . ");
      }
      else if(board[i][j] == PLAYER1)
      {
        printf(" o ");
      }
      else
      {
        printf(" x ");
      }
    }
    printf("\n");
  }
}

void PLAYERMove(int board[ROW][COL], int row, int col, int who)
{
  while(1)
  {
    printf("Player[%d] Please Enter Your Pos:>", who);
    scanf("%d %d", &x, &y);
    if(x>=1 && x<=row && y>=1 && y<= col)
    {
      if(board[x-1][y-1] == 0)
      {
        board[x-1][y-1] = who;
        break;
      }
      else 
      {
        printf("Pos Is Occupied!\n");
      }
    }
    else 
    {
      printf("Pos Is Not Right!\n");
    }
  }
}

int ChessCount(int board[ROW][COL], int row, int col, enum Dir d)
{
  int _x = x-1;
  int _y = y-1;
  int count = 0;
  while(1)
  {
    switch(d)
    {
      case LEFT:
        _y--;
        break;
      case RIGHT:
        _y++;
        break;
      case UP:
        _x--;
        break;
      case DOWN:
        _x++;
        break;
      case LEFT_UP:
        _x--,_y--;
        break;
      case LEFT_DOWN:
        _x++,_y--;
        break;
      case RIGHT_UP:
        _x--,_y++;
        break;
      case RIGHT_DOWN:
        _x++,_y++;
        break;
      default:
        //do nothing!
        break;
    }
    if(_x<0 || _x>row-1 || _y<0 || _y>col-1)
    {
      break;
    }
    if(board[x-1][y-1] == board[_x][_y])
    {
      count++;
    }
    else 
      break;
  }
  return count;
}

int IsOver(int board[ROW][COL], int row, int col)
{
  int count1 = ChessCount(board, row, col, LEFT) + ChessCount(board, row, col, RIGHT) + 1;
  int count2 = ChessCount(board, row, col, UP) + ChessCount(board, row, col, DOWN) + 1;
  int count3 = ChessCount(board, row, col, LEFT_UP) + ChessCount(board, row, col, RIGHT_DOWN) + 1;
  int count4 = ChessCount(board, row, col, LEFT_DOWN) + ChessCount(board, row, col, RIGHT_UP) + 1;
  if(count1>=5 || count2>=5 || count3>=5 || count4>=5)
  {
    if(board[x-1][y-1] == PLAYER1)
      return PLAYER1_WIN;
    else 
      return PLAYER2_WIN;
  }
  int k = 0;
  for(k = 0;k<row;k++)
  {
    int j = 0;
    for(j = 0;j<col;j++)
    {
      if(board[k][j] == 0)
        return NEXT;
    }
  }
  return DRAW;
}

void game()
{
  int board[ROW][COL];
  //initialize
  BoardInit(board, ROW, COL);
  int ret = NEXT;
  while(1)
  {
    BoardPrint(board, ROW, COL);
    PLAYERMove(board, ROW, COL, PLAYER1);
    ret = IsOver(board, ROW, COL);
    if(ret != NEXT)
      break;

    BoardPrint(board, ROW, COL);
    PLAYERMove(board, ROW, COL, PLAYER2);
    ret = IsOver(board, ROW, COL);
    if(ret != NEXT)
      break;
  }
  switch(ret)
  {
    case PLAYER1_WIN:
      BoardPrint(board, ROW, COL);
      printf("Player1 Win!!!\n");
      break;
    case PLAYER2_WIN:
      BoardPrint(board, ROW, COL);
      printf("Player2 Win!!!\n");
      break;
    case DRAW:
      BoardPrint(board, ROW, COL);
      printf("Draw!!!\n");
      break;
    default://do nothing!
      break;
  }
}
